package com.yin.myproject.thread.syncdemo;

/**
 * synchronized修饰静态方法
 *
 * @author Eason
 * @create 2018-05-09 14:53
 **/
public class SynchronizedDemo01 implements Runnable {

    private static int count;

    public SynchronizedDemo01() {
        count = 0;
    }

    public synchronized static void method() {
        for (int i = 0; i < 5; i ++) {
            try {
                System.out.println(Thread.currentThread().getName() + ":" + (count++));
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void run() {
        method();
    }

    public static void main(String[] args) {
        SynchronizedDemo01 syncThread1 = new SynchronizedDemo01();
        SynchronizedDemo01 syncThread2 = new SynchronizedDemo01();
        Thread thread1 = new Thread(syncThread1, "SyncThread1");
        Thread thread2 = new Thread(syncThread2, "SyncThread2");
        thread1.start();
        thread2.start();
    }
}
